from common.sqlalchemy_package import *
from flask import g


class Car_info(BaseModel):
    __tablename__ = 'car_info'
    car_id = mapped_column(Integer, primary_key=True)  #车辆id
    car_number = mapped_column(String)  # 车牌
    car_img_url = mapped_column(String)  # 车牌图片
    car_color = mapped_column(String)  # 颜色
    car_into_time=mapped_column(DateTime) #车辆进入时间
    car_leave_time = mapped_column(DateTime) #车辆出场时间
    money = mapped_column(Integer)#金额
    car_state=mapped_column(String)

    #获取全部车辆信息
    @staticmethod
    def query_car_info_all(page):
        per_page = 5  # 每页返回的数据量
        start_index = (int(page) - 1) * per_page  # 计算每页数据的起始索引
        query = g.db_session.query(Car_info)
        # 查询满足条件的记录总数
        total_count = query.count()
        # 计算总页数
        total_pages = (total_count + per_page - 1) // per_page
        # 分页查询并逆向排序
        results = query.order_by(Car_info.car_id.desc()).limit(per_page).offset(start_index).all()
        return results,total_pages
    #保存车辆信息
    @staticmethod
    def add_car_info(car_number,car_img_url,car_color,car_into_time,car_leave_time,money,car_state):
        try:
            car=Car_info(car_number=car_number,car_img_url=car_img_url,car_color=car_color,car_into_time=car_into_time,
                         car_leave_time=car_leave_time,money=money,car_state=car_state)
            g.db_session.add(car)
            g.db_session.commit()
            return 0
        except:
            return -1
    #根据车牌号查询信息
    @staticmethod
    def query_car_number_first(car_number):
        return g.db_session.query(Car_info).filter(Car_info.car_number == car_number).order_by(
            Car_info.car_id.desc()).first()

    #修改信息
    @staticmethod
    def modify_car_state(car_number,car_leave_time,money):
        try:
           query=g.db_session.query(Car_info).filter(Car_info.car_number == car_number).order_by(
            Car_info.car_id.desc()).first()
           query.car_state='False'
           query.car_leave_time=car_leave_time
           query.money=money
           g.db_session.commit()
           return 0
        except:
            return -1

    #修改离场时间
    @staticmethod
    def modify_car_leave_time(car_number, car_leave_time):
        try:
            query = g.db_session.query(Car_info).filter(Car_info.car_number == car_number).order_by(
                Car_info.car_id.desc()).first()
            query.car_state = 'False'
            query.car_leave_time = car_leave_time
            g.db_session.commit()
            return 0
        except:
            return -1

    #根据车牌号模糊搜索
    @staticmethod
    def car_number_search(car_number):
        return g.db_session.query(Car_info).filter(Car_info.car_number.like('%' + car_number + '%')).order_by(
            Car_info.car_id.desc()).all()

    # 根据车辆是否在库搜索
    @staticmethod
    def car_state_search(car_state):
        return g.db_session.query(Car_info).filter(Car_info.car_state.like('%' + car_state + '%')).order_by(
            Car_info.car_id.desc()).all()

    #修改在库状态
    @staticmethod
    def modify_car_is_state(car_id):
        try:
            query=g.db_session.query(Car_info).filter(Car_info.car_id == int(car_id)).first()
            if query:
                if query.car_state=='True':
                    query.car_state='False'
                else:
                    query.car_state='True'
                g.db_session.commit()
                return 0
            else:
                return -1
        except:
            return -1

    #根据车牌号查询该车辆是否在库
    @staticmethod
    def query_car_is(car_number):
        query = (g.db_session.query(Car_info).filter(Car_info.car_number == car_number).order_by
                 (Car_info.car_id.desc()).first())
        if query is not None:
            if query.car_state=='True':
                return 0 #在库
            else:
                return -1 #不在库
        else:
            return -1



